package tw.com.hitevision.whiteboard.android.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Objects;

import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.util.TextLayoutUtil;

public class NavigatorViewQRCodeFakeSpinner implements DialogInterface {

    private String TAG = NavigatorViewQRCodeFakeSpinner.class.getSimpleName();

    private Dialog dialog;
    private Activity activity;
    private ArrayList<String> list;
    private int currentPagesType;
    private NavigatorViewQRCodeExportDialog.FileFormat currentFileFormat;
    private ItemOnClickListener listener;
    private TextView selectedItem;

    NavigatorViewQRCodeFakeSpinner(Activity activity, ArrayList<String> list, NavigatorViewQRCodeExportDialog.FileFormat fileFormat, ItemOnClickListener listener) {
        this.activity = activity;
        this.list = list;
        this.currentFileFormat = fileFormat;
        this.listener = listener;
        dialog = new Dialog(activity, R.style.ExportDialog);
    }

    public Dialog getDialog() {
        return dialog;
    }

    void setCurrentPagesType(NavigatorViewQRCodeExportDialog.FileFormat currentFileFormat) {
        this.currentFileFormat = currentFileFormat;
    }

    public void show(int parentWidth, int parentHeight, float displacementX, float displacementY, int mainWidth) {
        dialog.setContentView(R.layout.navigator_qrcode_fake_spinner);
        // 811 dialog adjustment
        if(Props.isFake4k()) {
            WindowManager.LayoutParams layoutParams = Objects.requireNonNull(dialog.getWindow()).getAttributes();
            layoutParams.setTitle("ist_win_editdialog");
            dialog.getWindow().setAttributes(layoutParams);
        }
        Objects.requireNonNull(dialog.getWindow()).setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);

        View parentLayout = dialog.findViewById(R.id.qrcode_fake_parent_layout);
        parentLayout.setOnClickListener(view -> dismiss());
        View mainLayout = dialog.findViewById(R.id.navigator_qrcode_fake_spinner_layout);
        parentLayout.getLayoutParams().width = parentWidth;
        parentLayout.getLayoutParams().height = parentHeight;
        if(Props.isFake4k()) {
            mainLayout.setX(displacementX - TextLayoutUtil.convertDpToPixel(42, activity));
            mainLayout.setY(displacementY - TextLayoutUtil.convertDpToPixel(57, activity));
        } else {
            mainLayout.setX(displacementX - TextLayoutUtil.convertDpToPixel(43, activity));
            mainLayout.setY(displacementY - TextLayoutUtil.convertDpToPixel(34, activity));
        }
        selectedItem = dialog.findViewById(R.id.navigator_qrcode_export_option_selected_text);
//        mainLayout.getLayoutParams().width = mainWidth;
        dialog.setOnCancelListener(dialogInterface -> cancel());
        dialog.setOnDismissListener(dialogInterface -> dismiss());
        TextView item1 = dialog.findViewById(R.id.options_export_pages_fake_spinner_drop_down_item_1);
        item1.setText(list.get(0));
        item1.setOnClickListener(view -> {
            listener.itemOnClicked(0);
            dismiss();
        });
        TextView item2 = dialog.findViewById(R.id.options_export_pages_fake_spinner_drop_down_item_2);
        item2.setText(list.get(1));
        item2.setOnClickListener(view -> {
            listener.itemOnClicked(1);
            dismiss();
        });
        TextView item3 = dialog.findViewById(R.id.options_export_as_fake_spinner_drop_down_item_3);
        item3.setText(list.get(2));
        item3.setOnClickListener(view -> {
            listener.itemOnClicked(2);
            dismiss();
        });
        switch (currentFileFormat) {
            case PNG:{
                item1.setTextColor(activity.getResources().getColor(R.color.export_select_text_color));
                selectedItem.setText(list.get(0));
//                item1.setTypeface(null, Typeface.BOLD);
                break;
            }
            case JPEG:{
                item2.setTextColor(activity.getResources().getColor(R.color.export_select_text_color));
                selectedItem.setText(list.get(1));
//                item2.setTypeface(null, Typeface.BOLD);
                break;
            }
            case PDF:{
                item3.setTextColor(activity.getResources().getColor(R.color.export_select_text_color));
                selectedItem.setText(list.get(2));
//                item3.setTypeface(null, Typeface.BOLD);
                break;
            }
        }
        dialog.getWindow().getDecorView().setOnTouchListener((view, motionEvent) -> {
            Rect dialogBounds = new Rect();
            Objects.requireNonNull(dialog.getWindow()).getDecorView().getHitRect(dialogBounds);
            if (!dialogBounds.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
                dialog.dismiss();
            }
            return false;
        });
        dialog.show();
    }

    @Override
    public void cancel() {
        if(dialog.isShowing()) {
            dialog.cancel();
        }
    }

    @Override
    public void dismiss() {
        if(dialog.isShowing()) {
            dialog.dismiss();
        }
        listener.dialogDismiss();
    }

    interface ItemOnClickListener {
        void itemOnClicked(int position);
        void dialogDismiss();
    }
}
